package com.ynny.chapter02.controller;

import com.ynny.chapter02.dao.UserDaoImpl;
import com.ynny.chapter02.entity.User;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.Writer;

@WebServlet("/login")
public class LoginController extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request,
                           HttpServletResponse response) throws ServletException, IOException {


        String username = request.getParameter("username");
        String password = request.getParameter("pwd");

        UserDaoImpl userDao = new UserDaoImpl();

        //先要判断用户名称是否存在
        User us = userDao.selectUserByUserName(username);
        if (us == null){
            Writer writer = response.getWriter();
            writer.write("{\"code\" : 255, \"msg\" : \"fail userName is null \", \"data\": null}");

        }else {
            User user = userDao.selectUser(username, password);
            if (user == null){
                Writer writer = response.getWriter();
                writer.write("{\"code\" : 251, \"msg\" : \"fail password is not false \", \"data\": null}");

            }else {
                Writer writer = response.getWriter();
                writer.write("{\"code\" : 200, \"msg\" : \"login success \", \"data\": null}");

            }

        }



    }
}
